/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.imagesteganographylibrary.Utils;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;

/**
 * uri转pixmap
 *
 * @ProjectName: Image-Steganography-Library
 * @ClassName: ImageUtil
 * @Description: uri to pixmap
 * @Author: lr
 * @CreateDate: 2021/5/7 15:19
 * @since 2021-05-07
 */
public class ImageUtil {
    private static HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP,0x001,"ImageUtil");
    private static String fileName = "";

    private ImageUtil() {
    }

    /**
     * uri to pixmap
     *
     * @param context mContext
     * @param path    uri
     * @param type    0 or 1
     * @return pixelMap
     */
    public static PixelMap getPixMap(Context context, Uri path, int type) {
        PixelMap pixelMap = null;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();
        try {
            FileDescriptor fileDescriptor = helper.openFile(path, "r");
            ImageSource source = ImageSource.create(fileDescriptor, options);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            pixelMap = source.createPixelmap(decodingOptions);
            if (type == 1) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            // columns为null，查询记录所有字段，当前例子表示查询id字段
                            ResultSet resultSet = helper.query(path, null, null);
                            while (resultSet != null && resultSet.goToNextRow()) {
                                // 获取id字段的值
                                int column = resultSet.getColumnIndexForName(AVStorage.Images.Media.DISPLAY_NAME);
                                String name = resultSet.getString(column);
                                getFileName(name);
                            }
                        } catch (DataAbilityRemoteException e) {
                            HiLog.error(logLabel,e.getMessage());
                        }
                    }
                }.start();
            }
        } catch (DataAbilityRemoteException e) {
            HiLog.error(logLabel,e.getMessage());
        } catch (FileNotFoundException e) {
            HiLog.error(logLabel,e.getMessage());
        }
        return pixelMap;
    }

    /**
     * uri
     *
     * @param uri uri
     * @return uri
     */
    public static Uri getPath(Uri uri) {
        String path;
        StringBuilder stringBuilder = new StringBuilder();
        path = uri.toString();
        String str1 = path.substring(0, path.indexOf("media"));
        String str2 = path.substring(str1.length(), path.length());
        stringBuilder.append("dataability://" + "/").append(str2);

        return Uri.parse(stringBuilder.toString());
    }

    /**
     * get fileName
     *
     * @param name fileName
     */
    public static void getFileName(String name) {
        if (name != null && name.contains("#") || name.contains("==")) {
            setFileName(name.substring(0, name.indexOf("#")));
        }
    }

    public static String getFileName() {
        return fileName;
    }

    public static void setFileName(String fileName) {
        ImageUtil.fileName = fileName;
    }
}

